package com.example.ujsfish.model.manual;

import retrofit2.http.PUT;

public class ManualControlData {
    private static final int num = 2;

    private static final Integer MAXPWMINT = 70; //占空比最大值

    private static int leftWheel = 0;
    private static int rightWheel = 0;

    private volatile static ManualBody manualBody = null;

    //DataAnalysis单例模式
    private volatile static ManualControlData instance = null;
    private ManualControlData(){}

    public static ManualControlData getInstance(){
        if (instance == null){
            synchronized (ManualControlData.class){
                instance = new ManualControlData();
            }
        }
        return instance;
    }

    public static ManualBody getManualBody(){
        if (manualBody == null){
            synchronized (ManualControlData.class){
                manualBody = new ManualBody();
            }
        }
        return manualBody;
    }

    public static void setInstance(ManualControlData instance) {

        ManualControlData.instance = instance;
    }

    public static class ManualBody {

        private String left_wheel = "+00.0";//左明轮占空比
        private String right_wheel = "+00.0";//右明轮占空比
        private String feed = "00.0";//投饵抛盘占空比
        private String blanking = "00.0";//下料占空比
        private String valve = "00.0";//施药阀门占空比
        private String pump = "00.0";//施药液泵占空比
        private String device_name = "003000425850500C20353555"; //设备名

        private ManualBody(){};

        public void setPara(String device_name,String sell, String bait, String medicineValue, String medicinePump){
            this.device_name = device_name;
            this.feed = sell;
            this.blanking = bait;
            this.valve = medicineValue;
            this.pump = medicinePump;
        }

        /**
         * 将所有控制参数清零
         */
        public void clear(){
//            left_wheel = "+00.0";//左明轮占空比
//            right_wheel = "+00.0";//右明轮占空比
//            feed = "00.0";//投饵抛盘占空比
//            blanking = "00.0";//下料占空比
//            valve = "00.0";//施药阀门占空比
//            pump = "00.0";//施药液泵占空比

            left_wheel = "+00.0";//左明轮占空比
            right_wheel = "+00.0";//右明轮占空比
            feed = "00.0";//投饵抛盘占空比
            blanking = "00.0";//下料占空比
            valve = "00.0";//施药阀门占空比
            pump = "00.0";//施药液泵占空比

            leftWheel = 0;
            rightWheel = 0;


        }

        public String getFeed() {
            return feed;
        }

        public String getBlanking() {
            return blanking;
        }

        public String getValve() {
            return valve;
        }

        public String getPump() {
            return pump;
        }

        public String getDevice_name() {
            return device_name;
        }

        public void setDevice_name(String device_name) {
            this.device_name = device_name;
        }


        public String getLeft_wheel() {
            return left_wheel;
        }

        public String dataChanging(int temp){

            String change = "";

            if(temp >= 0){
                if (temp < 10 ){
                    change = "+0" + String.valueOf(temp) + ".0";
                 }else{
                    change = "+" + String.valueOf(temp) + ".0";
                }

            }else{
                if (temp > -10 ){
                    change = "-0" + String.valueOf(-temp) + ".0";
                }else{
                    change =  String.valueOf(temp) + ".0";
                }

            }



            return change;
        }

        public int limit(int wheel){
            if(wheel > MAXPWMINT){
                wheel = MAXPWMINT;
            }else if(wheel < -MAXPWMINT){
                wheel = -MAXPWMINT;
            }
            return wheel;
        }

        public void setLeftWheelAdd() {

            int temp = leftWheel;
            temp += num;
            leftWheel= limit(temp);
            this.left_wheel = dataChanging(leftWheel);

//            if (Integer.parseInt(this.left_wheel) > Integer.parseInt(MAXPWM)){
//                this.left_wheel = MAXPWM;
//            }
            System.out.println("left is :" + left_wheel);
            System.out.println("right is :" + right_wheel);

        }

        public void setLeftWheelDelete(){
            int temp = leftWheel;
            temp -= num;
            leftWheel= limit(temp);
            this.left_wheel = dataChanging(leftWheel);

//            this.left_wheel = String.valueOf(temp);
//            if (Integer.parseInt(this.left_wheel) < Integer.parseInt(MINPWM)){
//                this.left_wheel = MINPWM;
//            }
        }

        public String getRight_wheel() {
            return right_wheel;
        }

        public void setRightWheelAdd() {

            int temp = rightWheel;
            temp += num;
            rightWheel= limit(temp);
            this.right_wheel = dataChanging(rightWheel);
//            this.right_wheel = String.valueOf(temp);
//            if (Integer.parseInt(this.right_wheel) > Integer.parseInt(MAXPWM)){
//                this.right_wheel = MAXPWM;
//            }
            System.out.println("left is :" + left_wheel);
            System.out.println("right is :" + right_wheel);
        }

        public void setRightWheelDelete() {
            int temp = rightWheel;
            temp -= num;
            rightWheel= limit(temp);
            this.right_wheel = dataChanging(rightWheel);

//            this.right_wheel = String.valueOf(temp);
//            if (Integer.parseInt(this.right_wheel) < Integer.parseInt(MINPWM)){
//                this.right_wheel = MINPWM;
//            }
        }

        @Override
        public String toString() {
            return "ManualBody{" +
                    "left_wheel='" + left_wheel + '\'' +
                    ", right_wheel='" + right_wheel + '\'' +
                    ", feed='" + feed + '\'' +
                    ", blanking='" + blanking + '\'' +
                    ", valve='" + valve + '\'' +
                    ", pump='" + pump + '\'' +
                    ", device_name='" + device_name + '\'' +
                    '}';
        }
    }






}
